<?php

namespace App\Console\Commands;

use App\Library\Logic\ShopOrderLogic;
use Illuminate\Console\Command;

class ShopOrderDeliveryCommand extends Command
{
    /**
     * 订单发货
     *
     * @var string
     */
    protected $signature = 'command:shopOrderDelivery {serialNumber}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     * @throws \Exception
     */
    public function handle(): void
    {
        $serialNumber = $this->argument('serialNumber');
        if (empty($serialNumber) || strlen($serialNumber) !== 20) return;
        $order = ShopOrderLogic::findBySerialNumber($serialNumber);
        if (is_null($order)) return;
        else if (is_null($order->ext->pay_at)) return;
        else if ($order->order_buy_status !== 4 || $order->order_delivery_time === 0) return;
        else if (!is_null($order->deliver_at)) return;
        $order->ext->status = 3;
        $order->ext->status_deliver = 1;
        $order->ext->deliver_at = now();
        $order->ext->save();
        mq('shopOrderConfirm', $order->serial_number, (int)__('base.confirmTtl'));
    }
}
